const express = require('express')
const router = express.Router()
const db = require('../db')

router.post('/', (req, res) => {
  const { search_key } = req.query

  const searchByPhone = 'select * from easy_chat.users where phone = ?'
  db.query(searchByPhone, search_key, (err, results) => {
    if (err) return res.errHandle(err)

    if (results.length === 0) {
      res.send({
        code: 200,
        message: 'fail',
        data: {
          message: '该账号不存在'
        }
      })
      return
    }

    res.send({
      code: 200,
      message: 'success',
      data: {
        wx_id: results[0].wx_id,
        nickname: results[0].nickname,
        avatar_url: results[0].avatar_url
      }
    })
  })
})

module.exports = router
